import { createRouter, createWebHashHistory } from 'vue-router'
import {getToken} from "@/utils/auth.js";

const routes = [
  { path: '/', redirect: '/index' },
  {
    path: '/index',
    name: 'index',
    component: () => import('@/views/index'),
    meta: {
      title: 'FTF比赛平台'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/adminLogin'),
    meta: {
      title: '管理员登录'
    }
  },
  {
    path: '/scoreQuery',
    name: 'scoreQuery',
    component: () => import('@/views/scoreQuery'),
    meta: {
      title: '比赛分数查询'
    }
  },
  {
    path: '/upload-image',
    name: 'uploadImage',
    component: () => import('@/views/uploadImage.vue')
  },

  {
    path: '/tabs',
    name: 'tabs',
    component: () => import('@/views/tabs'),
    meta: {
      title: '',
      isAuth: true
    },
    redirect: '/tabs/participants',
    children: [
      {
        path: 'participants',
        name: 'participants',
        component: () => import('@/views/participants'),
        meta: {
          title: '参赛人员',
          isAuth: true
        }
      },
      {
        path: 'details',
        name: 'details',
        component: () => import('@/views/details'),
        meta: {
          title: '比赛详情',
          isAuth: true
        }
      }
    ]
  }
]

export const administerRouter = {}

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const isLoggedIn = getToken() === 'login-success!'
  if (to.meta.isAuth === true && !isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router
